<template>
	<view class="container tc">
		<image
			mode="aspectFit"
			class="logo"
			src="https://yilife-oss-online-public.oss-cn-hangzhou.aliyuncs.com/10000001/1015/ADMIN/uscr5ludvvlm646ovg1o106l1.png?timestamp=1676517127889"
		></image>
		<view class="f45 mt40 lh65"
			>我们需要根据您的手机号确认业务角色信息，为您提供对应功能服务</view
		>
		<view class="df jsa">
			<button class="mt30 primary-hollow-button pct45" @tap="cancelClick">
				拒绝授权
			</button>
			<button
				class="primary-button mt30 pct45"
				open-type="getPhoneNumber"
				@getphonenumber="decryptPhoneNumber"
			>
				授权登录
			</button>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			code: '',
		}
	},
	onLoad() {
		this.getCode()
	},
	methods: {
		getCode() {
			let that = this
			uni.login({
				provider: 'weixin',
				onlyAuthorize: true,
				success: function (loginRes) {
					that.code = loginRes.code
				},
			})
		},
		decryptPhoneNumber(e) {
			let that = this
			if (e.detail.code) {
				uni.setStorageSync('phoneNumberCode', e.detail.code)
				that.$request
					.request('user-web/wemini/parsingCode', {
						data: {
							jsCode: this.code,
						},
						method: 'post',
					})
					.then((res) => {
						if (res.success) {
							let { openId, unionId } = res.data
							uni.setStorageSync('openId', openId)
							uni.setStorageSync('unionId', unionId)
							let { iv, encryptedData } = e.detail
							that.$request
								.request('user-web/wemini/login', {
									data: {
										openId,
										iv,
										encryptedData,
										unionId,
										nickname: 1212,
										userType: 4,
									},
									method: 'post',
								})
								.then((res) => {
									if (res.success) {
										let data = res.data
										uni.setStorageSync('token', data.token)
										getApp().globalData.phone = data.phone
										uni.setStorageSync(
											'userPhone',
											data.phone
										)
										// that.orgId = data.belongId;
										// uni.setStorageSync("orgId", that.orgId);
										let rolesId =
											data.roles && data.roles.length > 0
												? data.roles.map(
														(item) => item.id
												  )
												: []
										uni.setStorageSync('rolesId', rolesId)
										uni.setStorageSync(
											'isApprover',
											data.isApprover
										)
										uni.setStorageSync('userId', data.id)
										uni.setStorageSync('loginUser', data)
										this.$request
											.request(
												'user-web/user/listTenant',
												{
													method: 'post',
													data: {},
												}
											)
											.then((res) => {
												if (res.success) {
													that.orgId =
														res.data[0].tenantId
													uni.setStorageSync(
														'orgId',
														that.orgId
													)
													uni.setStorageSync(
														'companyList',
														res.data
													)
													uni.setStorageSync(
														'selectedCompany',
														res.data[0]
													)
												}
											})
										// this.cancelClick();
										uni.navigateBack({
											delta: 1,
										})
									}
								})
						}
					})
			}
		},
		cancelClick() {
			uni.switchTab({
				url: '/pages/index/index',
			})
		},
	},
}
</script>

<style lang="scss" scoped>
.container {
	background-color: #ffffff;
	min-height: 100vh;
}
.logo {
	width: 200rpx;
	border-radius: 200rpx;
}
.lh65 {
	line-height: 76rpx;
}
button::after {
	border: none;
}
</style>
